/**
 * 日期工具函数
 */

/**
 * 格式化日期
 * @param {string|Date} date - 日期字符串或Date对象
 * @param {string} format - 格式化模板，默认为 'YYYY-MM-DD HH:mm:ss'
 * @returns {string} 格式化后的日期字符串
 */
export function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') {
  if (!date) return '-'
  
  const d = new Date(date)
  if (isNaN(d.getTime())) return '-'
  
  const year = d.getFullYear()
  const month = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  const hours = String(d.getHours()).padStart(2, '0')
  const minutes = String(d.getMinutes()).padStart(2, '0')
  const seconds = String(d.getSeconds()).padStart(2, '0')
  
  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds)
}

/**
 * 格式化相对时间
 * @param {string|Date} date - 日期字符串或Date对象
 * @returns {string} 相对时间字符串
 */
export function formatRelativeTime(date) {
  if (!date) return '-'
  
  const d = new Date(date)
  if (isNaN(d.getTime())) return '-'
  
  const now = new Date()
  const diff = now.getTime() - d.getTime()
  const seconds = Math.floor(diff / 1000)
  const minutes = Math.floor(seconds / 60)
  const hours = Math.floor(minutes / 60)
  const days = Math.floor(hours / 24)
  
  if (seconds < 60) {
    return '刚刚'
  } else if (minutes < 60) {
    return `${minutes}分钟前`
  } else if (hours < 24) {
    return `${hours}小时前`
  } else if (days < 7) {
    return `${days}天前`
  } else {
    return formatDate(date, 'YYYY-MM-DD')
  }
}

/**
 * 获取日期范围
 * @param {string} type - 类型：today, week, month, year
 * @returns {Array} [开始日期, 结束日期]
 */
export function getDateRange(type) {
  const now = new Date()
  const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
  
  switch (type) {
    case 'today':
      return [
        formatDate(today, 'YYYY-MM-DD'),
        formatDate(now, 'YYYY-MM-DD')
      ]
    
    case 'week': {
      const weekStart = new Date(today)
      weekStart.setDate(today.getDate() - today.getDay())
      return [
        formatDate(weekStart, 'YYYY-MM-DD'),
        formatDate(now, 'YYYY-MM-DD')
      ]
    }
    
    case 'month': {
      const monthStart = new Date(today.getFullYear(), today.getMonth(), 1)
      return [
        formatDate(monthStart, 'YYYY-MM-DD'),
        formatDate(now, 'YYYY-MM-DD')
      ]
    }
    
    case 'year': {
      const yearStart = new Date(today.getFullYear(), 0, 1)
      return [
        formatDate(yearStart, 'YYYY-MM-DD'),
        formatDate(now, 'YYYY-MM-DD')
      ]
    }
    
    default:
      return [null, null]
  }
}

/**
 * 判断是否为今天
 * @param {string|Date} date - 日期
 * @returns {boolean}
 */
export function isToday(date) {
  if (!date) return false
  
  const d = new Date(date)
  const today = new Date()
  
  return d.getFullYear() === today.getFullYear() &&
         d.getMonth() === today.getMonth() &&
         d.getDate() === today.getDate()
}

/**
 * 判断是否为本周
 * @param {string|Date} date - 日期
 * @returns {boolean}
 */
export function isThisWeek(date) {
  if (!date) return false
  
  const d = new Date(date)
  const now = new Date()
  const weekStart = new Date(now)
  weekStart.setDate(now.getDate() - now.getDay())
  weekStart.setHours(0, 0, 0, 0)
  
  return d >= weekStart
}

/**
 * 判断是否为本月
 * @param {string|Date} date - 日期
 * @returns {boolean}
 */
export function isThisMonth(date) {
  if (!date) return false
  
  const d = new Date(date)
  const now = new Date()
  
  return d.getFullYear() === now.getFullYear() &&
         d.getMonth() === now.getMonth()
}

export default {
  formatDate,
  formatRelativeTime,
  getDateRange,
  isToday,
  isThisWeek,
  isThisMonth
}